package com.xc.quickstart.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * Created by PC on 2017/3/23.
 */
public class TimeUtils {
    public static final String __ = "M月dd日 HH:mm";
    public static final String MMddHHmm = "MM-dd HH:mm";
    public static final String MMddHHmmss = "MM-dd HH:mm:ss";
    public static final String yyyyMMddStr = "yyyy-MM-dd";
    public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN_yyyyMMdd = "yyyyMMdd";
    public static final String DATE_PATTERN_yyyyMM = "yyyyMM";
    public static final String DATE_PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String DATE_PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String yyyyMMdd1 = "yyyyMMdd";
    public static final String yyyyMMddTT = "yyyy/MM/dd";
    public static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final LocalDateTime DEFAULT_TIME = parseStringtoLocalDatetime("2010-01-01 00:00:00", yyyyMMddHHmmss);


    /**
     * 格式化 <- 默认值，空
     *
     * @param date    时间
     * @param pattern 模式
     * @return
     */
    public static String format(Date date, String pattern) {
        return format(date, pattern, "");
    }

    public static String format(LocalDateTime date, String pattern) {
        return format(date, pattern, "");
    }

    /**
     * 格式化
     *
     * @param date    时间
     * @param pattern 模式
     * @param ifNull  默认值
     * @return
     */
    public static String format(Date date, String pattern, String ifNull) {
        if (Objects.isNull(date)) {
            return ifNull;
        }
        try {
            return
                    LocalDateTime
                            .ofInstant(date.toInstant(), ZoneId.systemDefault())
                            .format(DateTimeFormatter.ofPattern(pattern));
        } catch (Exception e) {
            return ifNull;
        }
    }

    public static String format(LocalDateTime date, String pattern, String ifNull) {
        if (Objects.isNull(date)) {
            return ifNull;
        }
        try {
            return date.format(DateTimeFormatter.ofPattern(pattern, Locale.CHINESE));
        } catch (Exception e) {
            return ifNull;
        }
    }

    public static Date parse(String date, String pattern) {
        return Date.from(
                LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern)).atZone(ZoneId.systemDefault()).toInstant());
    }

    public static int unixTime() {
        return (int) (System.currentTimeMillis() / 1000);
    }


    /**
     * 将long型时间值转为String类型
     * 用int 的时间戳一定要*1000L
     *
     * @param timestamp
     * @param pattern
     * @return
     */
    public static String format(long timestamp, String pattern) {

        return format(new Date(timestamp), pattern);
    }

    public static int localDateTime2unixTime(LocalDateTime date) {
        return (int) date.toEpochSecond(ZoneOffset.of("+8"));
    }


    public static LocalDateTime timeStamp2LocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static boolean isBeginOfDay(LocalDateTime dateTime) {
        if (dateTime.getHour() == 0 && dateTime.getMinute() == 0 && dateTime.getSecond() == 0) {
            return true;
        }
        return false;
    }

    public static Date parseFromUnix(int addTime) {
        return new Date((long) addTime * 1000);
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static LocalDateTime parseFromUnixToLocalTime(int addTime) {
        if (addTime <= 0) {
            addTime = TimeUtils.unixTime();
        }
        return LocalDateTime.ofInstant(new Date((long) addTime * 1000L).toInstant(), ZoneId.systemDefault());
    }

    public static LocalDateTime parseStringtoLocalDatetime(String date, String pattern) {
        return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate parseStringtoLocalDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }


    //计算两天日期之差
    public static int differDate(String timeBegin, String timeEnd) {
        Calendar startCal = Calendar.getInstance();
        Calendar endCal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate;
        Date endDate;
        try {
            startDate = sdf.parse(timeBegin);
            endDate = sdf.parse(timeEnd);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
        startCal.setTime(startDate);
        endCal.setTime(endDate);
        //得到两个日期相差的天数   还可通过去除后面的数 求得相差的小时数,分钟数,秒   等等
        int days = ((int) (endCal.getTime().getTime() / 1000) - (int) (startCal.getTime().getTime() / 1000)) / 3600 / 24;
        return days;
    }

    public static Date addDays(Date date1, long days) {
        return addSeconds(date1, days * 86400L);
    }

    private static Date addSeconds(Date date1, long secs) {
        return new Date(date1.getTime() + secs * 1000L);
    }

    // Date 转 LocalDateTime
    public static LocalDateTime transferToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
